package com.kyxx.topics.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.GradientDrawable;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kyxx.topics.R;
import com.kyxx.topics.bean.MembershipCardBean;
import com.kyxx.topics.net.UrlUtils;
import com.kyxx.topics.utils.StringUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class MembershipCardAdapter extends BaseQuickAdapter<MembershipCardBean,BaseViewHolder> {
    private Context ctx;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;
    private boolean is_member = false;
    public MembershipCardAdapter(Context paramContext, List<MembershipCardBean> paramList){
        super(R.layout.item_membership_card,paramList);
        this.ctx = paramContext;
        options = new DisplayImageOptions.Builder()
//				// 设置图片在下载期间显示的图片
                .showImageOnLoading(R.mipmap.icon_user_default)
//				// 设置图片Uri为空或是错误的时候显示的图片
                .showImageForEmptyUri(R.mipmap.icon_user_default)
//				// 设置图片加载/解码过程中错误时候显示的图片
                .showImageOnFail(R.mipmap.icon_user_default)
                .cacheInMemory(true)
                // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)
                // 设置下载的图片是否缓存在SD卡中
                .bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型
//                .displayer(new RoundedBitmapDisplayer(9999))
                .build();



    }



    @Override
    protected void convert(BaseViewHolder holder, final MembershipCardBean bean) {
        holder.setText(R.id.tv_member_name,bean.getType_name());
        if(!StringUtil.isEmpty(bean.getType_logo())){
            holder.setVisible(R.id.cv_member_icon,true)
                   .setVisible(R.id.tv_member_icon,false);
            imageLoader.displayImage(UrlUtils.BASEHTTP+bean.getType_logo(), (ImageView) holder.getView(R.id.cv_member_icon), options);
        }else {
            holder.setVisible(R.id.tv_member_icon,true)
                    .setVisible(R.id.cv_member_icon,false);
            holder.setText(R.id.tv_member_icon,bean.getType_name().substring(0,1));

            GradientDrawable drawable1 = (GradientDrawable)holder.getView(R.id.tv_member_icon).getBackground();
            if (bean.getShop_color() == null || bean.getShop_color().contains(",")) {
                bean.setShop_color("FFFDBB2B");
            } else {
                bean.setShop_color("FF" + bean.getShop_color());
            }
            int a = new java.math.BigInteger("ffffff", 16).intValue();
            int b = new java.math.BigInteger(bean.getShop_color(), 16).intValue();
            drawable1.setColor(b);
            if(a == b){
                holder.setTextColor(R.id.tv_member_icon,ctx.getResources().getColor(R.color.black));
            }else{
                holder.setTextColor(R.id.tv_member_icon,ctx.getResources().getColor(R.color.white));
            }
        }
        if(bean.getIs_member() == 1){
            holder.setImageResource(R.id.iv_member_num,R.mipmap.member_card);
        }else {
            holder.setImageResource(R.id.iv_member_num,R.mipmap.member_add);
        }
        GradientDrawable drawable = (GradientDrawable)holder.getView(R.id.rl_member).getBackground();
        if (bean.getShop_color() == null || bean.getShop_color().contains(",")) {
            bean.setShop_color("FFFDBB2B");
        } else {
            bean.setShop_color("FF" + bean.getShop_color());
        }
        int b = new java.math.BigInteger(bean.getShop_color(), 16).intValue();
        drawable.setColor(b);
        holder.addOnClickListener(R.id.rl_member_num);

        if(bean.getFlag()==1){
            holder.setVisible(R.id.iv_is_notify,true);
        }else{
            holder.setVisible(R.id.iv_is_notify,false);
        }
    }

}